<?php

class ModuleResultOfHotSearchKeywords extends CmsModule {
    //set the public head
    private function setHeadString($condition=null,$sort_by=null,$rand_code) {
        $date_list = null;
        $product_list = null;
        $top_list = null;
        $current_time = date('Y-m-d H:i:s');
        if (!empty($condition['date_by']) && !empty($condition['date_time'])) {
            $date_list = '<b>By '.ucwords($condition['date_by']).":&nbsp;</b>".$condition['date_time'];
        }
        if (!empty($condition['product'])) {
            $product_list = '<b>Product:&nbsp;</b>'.$condition['product'];
        }
        if (!empty($condition['top_limit'])) {
            $top_list = '<b>Top:&nbsp;</b>'.$condition['top_limit'];
        }
        $condition_list = '<br/>'.
                          '<div>'.$date_list.'</div>'.
                          '<div>'.$product_list.'</div>'.
                          '<div>'.$top_list.'</div>';

       //control tag.                         
        $export_tag =  'Export to '.AC::exportHtmlControl($rand_code,AC::EXPORT_FILE_PREFIX_NAME_HOT_SEARCH)
                                   .AC::exportPdfControl($rand_code,AC::EXPORT_FILE_PREFIX_NAME_HOT_SEARCH)
                                   .AC::exportCvsControl($rand_code,AC::EXPORT_FILE_PREFIX_NAME_HOT_SEARCH);

        $head_string = '<div class="hd">'.
                       '    <div class="utility">'.
                       '        <a href="#" class="print">Print</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$export_tag.
                       '    </div>'.
                       '    <hr>'.
                       '    <h1>'.$this->title.'</h1>'.
                       '    <div class="ui-helper-clearfix">'.
                       '        <div class="time">'.$current_time.'</div>'.
                       '        <ul>'.$condition_list.'</ul>'.
                       '    </div>'.
                       '</div>';

        $head_string_exprot = '<div class="hd">'.
                                '    <div class="utility">'.
                                '        <a href="#" class="print">Print</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.
                                '    </div>'.
                                '    <hr>'.
                                '    <h1>'.$this->title.'</h1>'.
                                '    <div class="ui-helper-clearfix">'.
                                '        <div class="time">'.$current_time.'</div>'.
                                '        <ul>'.$condition_list.'</ul>'.
                                '    </div>'.
                                '</div>';

        return array('view'=>$head_string,'export'=>$head_string_exprot);
    }    

    //out put the body string.
    private function getOutPutBody($head_string,$body_string) {
        $output ='<div id="result" class="hot_search_keyword_result" >'.$head_string.
                 '  <div class="bd">'.
                 '  <br />'.
                 '  <table width="100%">'.
                    $body_string.
                 '  </table>'.
                 '  </div>'.
                 '</div>';
        return $output;
    }
    
    //get the page search filter condition.
    private function getPageCondition($params) {
        $date_by = (!empty($params['dateby'])) ? $params['dateby'] : 'month';
        
        switch($date_by) {
            case 'month':
                $date_time = $params['year'];
                break;
            case 'date':
                $date_time = $params['year'].'-'.$params['month'];
                break;
            case 'hour':
                $date_time = $params['year'].'-'.$params['month'].'-'.$params['day'];
                break;
        }
        
        $product = 'all';
        if (isset($params['product'])) {
            $product = $params['product'];
        }

        $top_limit = '100';
        if (isset($params['top'])) {
            $top_limit = (!empty($params['top'])) ? $params['top'] : $top_limit;
        }
        return array('condition'=>array('date_by'=>$date_by,'date_time'=>$date_time,'product'=>$product,'top_limit'=>$top_limit));
    }
    
    private function setHeaderString ($condition) {
        $search_title = ($condition=='month') ? '年' : (($condition=='date') ? '月' : '日');
        $body_list = '<tr class="weight" bgcolor="#CCC">'.
                     '  <td width="10%">&nbsp;</td>'.
                     '  <td>排名</td>'.
                     '  <td>關鍵字</td>'.
                     '  <td align="right">'.$search_title.'搜索</td>'.
                     '  <td align="right">歷史總量搜索</td>'.
                     '  <td width="10%">&nbsp;</td>'.
                     '</tr>';
        return $body_list;
    }

    //set the body string.
    private function setBodyString($date_time,$date_by,$data) {
        $body_list = null;
        foreach($data as $key=>$val) {
            $body_list .=   '<tr>'.
                            '   <td>&nbsp;</td>'.
                            '   <td>'.$key.'</td>'.
                            '   <td>'.$val['keyword'].'</td>'.
                            '   <td align="right">'.number_format($val['daily']).'</td>'.
                            '   <td align="right">'.number_format($val['history']).'</td>'.
                            '   <td>&nbsp;</td>'.
                            '</tr>';
        }
       
        return $body_list;
    }

    private function setNoneString($message) {
        return '<tr height="80"><td colspan="6" align="center">'.$message.'</td></tr>';
    }
    
    private function getBodyDataList($condition) {
        $date_by = $condition['date_by'];
        $date_time = $condition['date_time'];
        $product = $condition['product'];
        $top_limit = $condition['top_limit'];
        $obj = new ModelReportPages();
        $data = $obj->getHotSearchKeywordData($date_by,$date_time,$product,$top_limit);
        return $data;
    }
   
    private function setHotSearchKeywordBodyString($condition) {
        $body_list = $this->setHeaderString($condition['date_by']);
        $data = $this->getBodyDataList($condition);
        $body_list .= (!empty($data)) ? $this->setBodyString($condition['date_time'],$condition['date_by'],$data) : $this->setNoneString(ErrorMessage::HAVE_NO_DATA);
        return $body_list;
    }


    //entrance.
    protected function __renderHead() {
        $body_string = null;
        $rand_code = rand(10000,99999);

        //parameters re-assignment.
        $conditions = $_GET;
        $conditions = $this->getPageCondition($conditions);

        //get the header string.
        $head_string = $this->setHeadString($conditions['condition'],null,$rand_code);
        
        //body string.
        $body_string .= $this->setHotSearchKeywordBodyString($conditions['condition']);

        //write a tmp file Export Tool.
        $export_html_string = $this->getOutPutBody($head_string['export'],$body_string);
        $write_result = AC::getInstance()->writeTempBodyToFile($rand_code,$export_html_string);
        
        //output.
        $this->contents =$this->getOutPutBody($head_string['view'],$body_string);
        
    }
}

?>
